// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.parts;

import java.beans.PropertyChangeEvent;

import org.dengues.reports.editor.design.border.CellBorder;
import org.dengues.reports.editor.design.figures.CellFigure;
import org.dengues.reports.editor.design.layout.ReportFlowLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Insets;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-19 qiang.zhang $
 * 
 */
public class TableCellEditPart extends AbstractCellEditPart {

    /**
     * Qiang.Zhang.Adolf@gmail.com TableCellEditPart constructor comment.
     * 
     * @param model
     */
    public TableCellEditPart(Object model) {
        super(model);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        CellFigure figure = new CellFigure();
        ReportFlowLayout rflayout = new ReportFlowLayout();
        figure.setLayoutManager(rflayout);
        figure.setOpaque(false);
        return figure;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutCell#getColSpan()
     */
    public int getColSpan() {
        return 10;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutCell#getColumnNumber()
     */
    public int getColumnNumber() {
        return 3;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutCell#getRowNumber()
     */
    public int getRowNumber() {
        return 3;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.layout.ITableLayoutCell#getRowSpan()
     */
    public int getRowSpan() {
        return 10;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent evt) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.parts.ReportElementEditPart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.parts.ReportElementEditPart#refreshFigure()
     */
    @Override
    public void refreshFigure() {
        CellBorder cborder = new CellBorder();

        if (getFigure().getBorder() instanceof CellBorder) {
            cborder.setBorderInsets(((CellBorder) getFigure().getBorder()).getBorderInsets());
        }
        // refreshBorder(getCellAdapter().getHandle(), cborder);

        Insets ist = new Insets(1);

        // ((CellBorder) (getFigure().getBorder())).setPaddingInsets(ist);

        // StyleHandle style = ((CellHandle) getModel()).getPrivateStyle();

        // String hAlign = style.getTextAlign();
        // String vAlign = style.getVerticalAlign();

        ReportFlowLayout rflayout = (ReportFlowLayout) getFigure().getLayoutManager();

        // if (DesignChoiceConstants.TEXT_ALIGN_CENTER.equals(hAlign)) {
        // rflayout.setMajorAlignment(ReportFlowLayout.ALIGN_CENTER);
        // } else if (DesignChoiceConstants.TEXT_ALIGN_RIGHT.equals(hAlign)) {
        // rflayout.setMajorAlignment(ReportFlowLayout.ALIGN_RIGHTBOTTOM);
        // } else {
        // rflayout.setMajorAlignment(ReportFlowLayout.ALIGN_LEFTTOP);
        // }
        //
        // if (DesignChoiceConstants.VERTICAL_ALIGN_MIDDLE.equals(vAlign)) {
        // rflayout.setMinorAlignment(ReportFlowLayout.ALIGN_CENTER);
        // } else if (DesignChoiceConstants.VERTICAL_ALIGN_BOTTOM.equals(vAlign)) {
        // rflayout.setMinorAlignment(ReportFlowLayout.ALIGN_RIGHTBOTTOM);
        // } else {
        // rflayout.setMinorAlignment(ReportFlowLayout.ALIGN_LEFTTOP);
        // }

        rflayout.layout(getFigure());

        updateBlankString();

        // refreshBackground((DesignElementHandle) getModel());
    }

    /**
     * Draws the string when the cell is empty
     */
    public void updateBlankString() {
        ((CellFigure) getFigure()).setBlankString(null);
    }
}
